package bb.lanxing.util;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Picture;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class BitmapHelper {
    public static byte[] bitmapToByteArray(Bitmap bitmap, final boolean needRecycle, Bitmap.CompressFormat format) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(format, 100, byteArrayOutputStream);
        if (needRecycle) {
            bitmap.recycle();
        }
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        try {
            byteArrayOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return byteArray;
    }

    public static byte[] bitmapToByteArray(Bitmap bitmap, final boolean needRecycle) {
        return bitmapToByteArray(bitmap, needRecycle, Bitmap.CompressFormat.PNG);
    }

    public static Bitmap byteArrayToBitmap(byte[] byteArray) {
        if (byteArray == null) {
            return null;
        }
        return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    }

    public static Bitmap shrinkBitmap(Bitmap bm, int maxLengthOfEdge) {
        return shrinkBitmap(bm, maxLengthOfEdge, 0);
    }

    public static Bitmap shrinkBitmap(Bitmap bm, int maxLengthOfEdge, int rotateXDegree) {
        float f;
        int width;
        if (maxLengthOfEdge > bm.getWidth() && maxLengthOfEdge > bm.getHeight()) {
            if (rotateXDegree == 0) {
                return bm;
            }
            Matrix matrix = new Matrix();
            matrix.postRotate(rotateXDegree);
            return Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
        }
        if (bm.getHeight() > bm.getWidth()) {
            f = maxLengthOfEdge;
            width = bm.getHeight();
        } else {
            f = maxLengthOfEdge;
            width = bm.getWidth();
        }
        float f2 = f / width;
        Matrix matrix2 = new Matrix();
        matrix2.postScale(f2, f2);
        matrix2.postRotate(rotateXDegree);
        return Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix2, true);
    }

    public static Bitmap readBitmap(Uri selectedImage) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        options.inPurgeable = true;
        options.inInputShareable = true;
        return BitmapFactory.decodeFile(selectedImage.getPath(), options);
    }

    public static Bitmap readSampleBitmap(Uri orgImage, int maxLengthOfEdge) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(orgImage.getPath(), options);
        int i = options.outWidth;
        int i2 = options.outHeight;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        options.inJustDecodeBounds = false;
        if (maxLengthOfEdge > i && maxLengthOfEdge > i2) {
            return BitmapFactory.decodeFile(orgImage.getPath(), options);
        }
        options.inSampleSize = (int) (((Math.max(i2, i)) / maxLengthOfEdge) + 0.5d);
        return BitmapFactory.decodeFile(orgImage.getPath(), options);
    }

    public static Bitmap getViewBitmap(View view) {
        if (view.getWidth() <= 0 || view.getHeight() <= 0) {
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        }
        int width = view.getWidth();
        int height = view.getHeight();
        if (width <= 0 || height <= 0) {
            return null;
        }
        Bitmap createBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        view.draw(new Canvas(createBitmap));
        return createBitmap;
    }

    public static Bitmap getWebviewBitmap(WebView webView) {
        Picture capturePicture = webView.capturePicture();
        Bitmap createBitmap = Bitmap.createBitmap(capturePicture.getWidth(), capturePicture.getHeight(), Bitmap.Config.ARGB_8888);
        capturePicture.draw(new Canvas(createBitmap));
        return createBitmap;
    }

    public static Bitmap captureWebViewLollipop(WebView webView) {
        Bitmap createBitmap = Bitmap.createBitmap(webView.getWidth(), (int) ((webView.getContentHeight() * webView.getScale()) + 0.5d), Bitmap.Config.ARGB_8888);
        webView.draw(new Canvas(createBitmap));
        return createBitmap;
    }

    public static Bitmap resampleBitmap(Bitmap bitmap, float scale) {
        if (bitmap == null) {
            return null;
        }
        return (scale <= 0.0f || scale >= 1.0f) ? bitmap : resampleBitmap(bitmap, (int) (bitmap.getWidth() * scale), (int) (bitmap.getHeight() * scale));
    }

    public static Bitmap resampleBitmap(Bitmap bitmap, int width, int height) {
        if (width > 0 || height > 0) {
            int width2 = bitmap.getWidth();
            int height2 = bitmap.getHeight();
            float f = (float) width / width2;
            float f2 = (float) height / height2;
            if (width <= 0) {
                f = f2;
            } else if (height <= 0) {
                f2 = f;
            }
            Matrix matrix = new Matrix();
            matrix.postScale(f, f2);
            return Bitmap.createBitmap(bitmap, 0, 0, width2, height2, matrix, true);
        }
        return bitmap;
    }

    public static boolean saveBitmap(Bitmap bitmap, String fileName) {
        if (fileName == null || bitmap == null) {
            return false;
        }
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(fileName);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
            return true;
        } catch (IOException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    public static Bitmap text2Bitmap(String text, float textSize, int textColor, int bgColor, int offsetBottom) {
        DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
        float applyDimension = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, textSize, displayMetrics);
        TextPaint textPaint = new TextPaint(65);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setColor(textColor);
        textPaint.setTextSize(applyDimension);
        Rect rect = new Rect();
        textPaint.getTextBounds(text, 0, text.length(), rect);
        StaticLayout staticLayout = new StaticLayout(text, textPaint, rect.width() + 10, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, true);
        float f = offsetBottom + 0.0f;
        float applyDimension2 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4.0f, displayMetrics);
        float applyDimension3 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, f, displayMetrics);
        float applyDimension4 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1.3f, displayMetrics);
        float applyDimension5 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0.0f, displayMetrics);
        float applyDimension6 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3.0f, displayMetrics);
        float applyDimension7 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3.0f, displayMetrics);
        float f2 = applyDimension2 + applyDimension4;
        float f3 = applyDimension3 + applyDimension5;
        float width = staticLayout.getWidth() + applyDimension4;
        float height = staticLayout.getHeight() + applyDimension5;
        Bitmap createBitmap = Bitmap.createBitmap((int) (staticLayout.getWidth() + f2), (int) (staticLayout.getHeight() + f3), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap);
        Paint paint = new Paint();
        paint.setColor(bgColor);
        RectF rectF = new RectF(0.0f, 0.0f, width, height);
        canvas.save();
        canvas.translate(applyDimension2 / 2.0f, applyDimension3 / 2.0f);
        canvas.drawRoundRect(rectF, applyDimension6, applyDimension7, paint);
        canvas.restore();
        canvas.save();
        canvas.translate(f2 / 2.0f, f3 / 2.0f);
        staticLayout.draw(canvas);
        canvas.restore();
        return createBitmap;
    }

    public static void recycleImageViewBitmap(ImageView imageView) {
        if (imageView != null) {
            imageView.setImageBitmap(null);
            imageView.destroyDrawingCache();
            rceycleBitmapDrawable((BitmapDrawable) imageView.getDrawable());
        }
    }

    public static void recycleBackgroundBitmap(ImageView view) {
        if (view != null) {
            rceycleBitmapDrawable((BitmapDrawable) view.getBackground());
            view.setBackgroundDrawable(null);
            view.getBackground().setCallback(null);
        }
    }

    public static void rceycleBitmapDrawable(BitmapDrawable bitmapDrawable) {
        if (bitmapDrawable != null) {
            recycleBitmap(bitmapDrawable.getBitmap());
        }
    }

    public static void recycleBitmap(Bitmap bitmap) {
        if (bitmap == null || bitmap.isRecycled()) {
            return;
        }
        bitmap.recycle();
    }

    public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
        int intrinsicWidth = drawable.getIntrinsicWidth();
        int intrinsicHeight = drawable.getIntrinsicHeight();
        Bitmap drawableToBitmap = drawableToBitmap(drawable);
        Matrix matrix = new Matrix();
        matrix.postScale((float) w / intrinsicWidth, (float) h / intrinsicHeight);
        return new BitmapDrawable((Resources) null, Bitmap.createBitmap(drawableToBitmap, 0, 0, intrinsicWidth, intrinsicHeight, matrix, true));
    }

    public static Bitmap drawableToBitmap(Drawable drawable) {
        int intrinsicWidth = drawable.getIntrinsicWidth();
        int intrinsicHeight = drawable.getIntrinsicHeight();
        Bitmap createBitmap = Bitmap.createBitmap(intrinsicWidth, intrinsicHeight, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(createBitmap);
        drawable.setBounds(0, 0, intrinsicWidth, intrinsicHeight);
        drawable.draw(canvas);
        return createBitmap;
    }
}
